package com.zhengsr.wanandroid.mvp.present;

import android.text.TextUtils;

import com.zhengsr.corelib.bean.ArticleCollectBean;
import com.zhengsr.corelib.bean.BaseResponse;
import com.zhengsr.corelib.bean.LogUpBean;
import com.zhengsr.corelib.bean.LoginBean;
import com.zhengsr.corelib.utils.RxUtils;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.base.IBaseView;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.model.DataManager;
import com.zhengsr.wanandroid.widget.CusSubscribe;

/**
 * Created by zhengshaorui
 * time: 2018/10/6
 */

public class LoginPresent extends BasePresent<ContractManager.ILoginView> {

    private IBaseView mView;
    private DataManager mDataManager;
    public static LoginPresent create(IBaseView view) {
        return new LoginPresent(view);
    }

    private LoginPresent(IBaseView view) {
        mView = view;
        mDataManager =  DataManager.getInstance();
    }





    public void getLoginIn(final String name, final String password) {
        if (name.isEmpty() || password.isEmpty()){
            mView.showErrorMsg("用户名或者密码不能为空");
            return;
        }
        addSubscribe(mDataManager.loginIn(name, password)
                .compose(RxUtils.<BaseResponse<LoginBean>>rxScheduers())
                .compose(RxUtils.<LoginBean>handleResult())
                .subscribeWith(new CusSubscribe<LoginBean>(mView) {
                    @Override
                    public void onNext(LoginBean loginBean) {
                        setLoginStatus(true);
                        setLoginName(name);
                        setLoginPassword(password);
                        if (mView instanceof ContractManager.ILoginView){
                            ContractManager.ILoginView iView = (ContractManager.ILoginView) mView;
                            iView.loginInSuccess(loginBean);
                        }
                      //  mView.loginInSuccess(loginBean);

                    }
                })
        );

    }

    public void register(String userName,String password,String rePassowrd){
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password) || TextUtils.isEmpty(rePassowrd)){
            mView.showErrorMsg("用户名或者密码不能为空");
            return;
        }

        addSubscribe(
                mDataManager.register(userName,password,rePassowrd)
                .compose(RxUtils.<BaseResponse<LogUpBean>>rxScheduers())
                .compose(RxUtils.<LogUpBean>handleResult())
                .subscribeWith(new CusSubscribe<LogUpBean>(mView) {
                    @Override
                    public void onNext(LogUpBean logUpBean) {
                        if (mView instanceof ContractManager.ILogUpView){
                            ContractManager.ILogUpView iView = (ContractManager.ILogUpView) mView;
                            iView.logUpSuccess(logUpBean);
                        }
                    }
                })
        );
    }

    public void loginOut(){
        addSubscribe(
                mDataManager.loginOut()
                .compose(RxUtils.<BaseResponse<ArticleCollectBean>>rxScheduers())
                .subscribeWith(new CusSubscribe<BaseResponse<ArticleCollectBean>>(mView) {
                    @Override
                    public void onNext(BaseResponse<ArticleCollectBean> articleCollectBeanBaseResponse) {
                        setLoginStatus(false);
                        setLoginName("");
                        setLoginPassword("");
                      //  mView.loginOutSuccess();
                        if (mView != null) {
                            if (mView instanceof ContractManager.ILogoutView) {
                                ContractManager.ILogoutView iView = (ContractManager.ILogoutView) mView;
                                iView.loginOutSuccess();
                            }
                        }
                    }
                })
        );

    }

}
